package cn.cai.pethome.dept.service.impl;

import cn.cai.pethome.base.service.impl.BaseServiceImpl;
import cn.cai.pethome.dept.domain.Department;
import cn.cai.pethome.dept.mapper.IDepartmentMapper;
import cn.cai.pethome.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private IDepartmentMapper departmentMapper;



    @Transactional
    @Override
    public void add(Department department) {

        departmentMapper.add(department);
        this.update(department);

    }

    @Transactional
    @Override
    public void update(Department department) {
        String dirPath = null;
        if (department.getParent() != null){   //判断上级部门是否空   Parent是前端传过来的数据
            Long id = department.getParent().getId();//获取上级id
            Department parenDept = this.findById(id);//根据上级id查询
            dirPath = parenDept.getDirPath();//根据上级id查询出来数据中取出DirPath的id
        }
        Long id = department.getId();//如果上级部门为空
        dirPath = dirPath + "/" + id;//直接在DirPath字段上拼接
        department.setDirPath(dirPath);//在把拼接好的字符串放在父亲表的dirPath字段上
        departmentMapper.update(department);//最后修改数组就行了
    }


    @Override
    public List<Department> getTree() {
         //第一步查询所有部门
        List<Department> deptAll = departmentMapper.findAll();
        //专门用来装顶级部门的
        List<Department> firstDept = new ArrayList<>();
        for (Department department : deptAll) {
            //判断parent_id == null 意思就顶级部门
            if (department.getParent_id() == null){
                firstDept.add(department);

            }else {
                //不是顶级部门
                    for (Department parentDept : deptAll) {
                        //下级找上级
                        if (department.getParent_id() == parentDept.getId()){
                            parentDept.getChildren().add(department);
                        }
                    }
            }
        }
        return firstDept;
    }
/*    @Override
    public List<Department> getTree2() {
        //第一步查询所有部门
        List<Department> deptAll = departmentMapper.findAll();
        Map<Long,Department> parentDeptMap = new HashMap<>();

        for (Department department : deptAll) {
            parentDeptMap.put(department.getId(),department);
        }

        //专门用来装顶级部门的
        List<Department> firstDept = new ArrayList<>();
        for (Department department : deptAll) {
            if (department.getParent_id() == null){
                firstDept.add(department);
            }else {
                Long parent_id = department.getParent_id();
                Department pDept = parentDeptMap.get(parent_id);
                pDept.getChildren().add(department);
            }
        }
        
        return firstDept;
    }*/
}
